﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MrBrooks.Utility;

namespace MrBrooks.DemoTcpClient
{
    public partial class frmMain : Form
    {
        public frmMain()
        {
            InitializeComponent();
        }

        private void btnTest_Click(object sender, EventArgs e)
        {
            var list = CardHelper.RandomCards(2);
            txtLogs.Text = string.Join(",", list.Select(p => string.Format("{0}{1}", p.Flower, p.Value)).ToArray());
        }
    }

    /// <summary>
    /// 扑克牌工具集
    /// </summary>
    public class CardHelper
    {
        /// <summary>
        /// 牌型列表
        /// </summary>
        private static readonly string[] Values = new string[] { "A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K" };

        /// <summary>
        /// 随机几副牌
        /// </summary>
        /// <param name="count"></param>
        /// <returns></returns>
        public static IList<Card> RandomCards(int count)
        {
            IList<Card> list = new List<Card>();
            if (count <= 0) return list;

            var length = Values.Length;
            int amount = length * count;

            foreach (var k in Enum.GetValues(typeof(FlowerEnum)))
            {
                for (int i = 0; i < amount; i++)
                {
                    var d = new Card
                    {
                        Flower = (FlowerEnum) k,
                        Value = Values[i % length],
                        RandomKey = Transfer.RandomKey(10)
                    };
                    list.Add(d);
                }
            }

            return (from p in list orderby p.RandomKey select p).ToList();
        }
    }

    /// <summary>
    /// 一张牌
    /// </summary>
    [Serializable]
    public class Card
    {
        /// <summary>
        /// 花色
        /// </summary>
        public FlowerEnum Flower { get; set; }

        /// <summary>
        /// 牌号
        /// </summary>
        public string Value { get; set; }

        /// <summary>
        /// 排序值
        /// </summary>
        public string RandomKey { get; set; }
    }

    /// <summary>
    /// 牌型
    /// </summary>
    public enum FlowerEnum
    {
        /// <summary>
        /// 黑桃
        /// </summary>
        A,

        /// <summary>
        /// 红桃
        /// </summary>
        B,

        /// <summary>
        /// 梅花
        /// </summary>
        C,

        /// <summary>
        /// 方块
        /// </summary>
        D
    }
}
